package com.ldcc.report_system.ui.report;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.widget.ViewPager2;

import com.ldcc.report_system.ApplicationConfig;
import com.ldcc.report_system.R;
import com.ldcc.report_system.ReportApplication;
import com.ldcc.report_system.adapter.FragmentViewPagerAdapter;
import com.ldcc.report_system.databinding.FragmentReportListBinding;

import java.util.ArrayList;


/**
 * @author wwj[yeshengkele@outlook.com]
 * @description 查看日报界面
 */
public class ReportListFragment extends Fragment {

    private FragmentReportListBinding reportListBinding;

    private FragmentActivity activity;
    // 当前选项卡字体
    private TextView onIndexTabText;
    // 当前选项卡下标图标
    private ImageView onIndexTabIcon;


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.reportListBinding = FragmentReportListBinding.inflate(inflater, container, false);
        this.activity = getActivity();
        // 初始化
        this.init();
        return this.reportListBinding.getRoot();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private void init() {
        // 当前选项卡赋值
        this.onIndexTabText = this.reportListBinding.dayReportText;
        this.onIndexTabIcon = this.reportListBinding.dayReportIcon;
        // 默认日报高亮
        this.tabSelected(this.reportListBinding.dayReportText, this.reportListBinding.dayReportIcon);

        // 初始化viewPager
        this.reportListBinding.reportListViewPager.setAdapter(new FragmentViewPagerAdapter(
                new ArrayList<Fragment>() {{
                    add(new DayListFragment());
                    add(new WeekListFragment());
                }}, this.activity.getSupportFragmentManager(), getLifecycle()
        ));
        // 按钮事件
        this.reportListBinding.dayReportBtn.setOnClickListener(view -> {
            reportListBinding.reportListViewPager.setCurrentItem(ApplicationConfig.DAY_LIST_FRAGMENT);
            this.tabSelected(this.reportListBinding.dayReportText, this.reportListBinding.dayReportIcon);
        });

        this.reportListBinding.weekReportBtn.setOnClickListener(view -> {
            reportListBinding.reportListViewPager.setCurrentItem(ApplicationConfig.WEEK_LIST_FRAGMENT);
            this.tabSelected(this.reportListBinding.weekReportText, this.reportListBinding.weekReportIcon);
        });
        // 滑动事件
        this.reportListBinding.reportListViewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                if (position == ApplicationConfig.WEEK_LIST_FRAGMENT) {
                    reportListBinding.reportListViewPager.setCurrentItem(ApplicationConfig.WEEK_LIST_FRAGMENT);
                    tabSelected(reportListBinding.weekReportText, reportListBinding.weekReportIcon);
                } else {
                    reportListBinding.reportListViewPager.setCurrentItem(ApplicationConfig.DAY_LIST_FRAGMENT);
                    tabSelected(reportListBinding.dayReportText, reportListBinding.dayReportIcon);
                }
            }
        });
    }

    /**
     * @param textView  tab栏文字
     * @param imageView tab 栏图标
     * @description tab栏选中事件
     */
    private void tabSelected(TextView textView, ImageView imageView) {
        // 复位
        this.onIndexTabText.setTextColor(ContextCompat.getColor(ReportApplication.getContext(), R.color.tab_normal));
        this.onIndexTabIcon.setBackgroundResource(0);
        //选中
        textView.setTextColor(ContextCompat.getColor(ReportApplication.getContext(), R.color.tab_activation));
        imageView.setBackgroundResource(R.drawable.tab_select_index);
        //赋值
        this.onIndexTabText = textView;
        this.onIndexTabIcon = imageView;
    }
}